package cc.wsyw126.java.collection.set;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by junpeng.yang on 16/9/1.
 */
public class IteratorToArray {
    public HashSet<Integer> addHashSet(){
        long start = System.currentTimeMillis();
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < 1000000; i++) {
            set.add(i);
        }
        System.out.println("IteratorToArray.addHashSet  :" + (System.currentTimeMillis() - start));
        return set;
    }


    public void useIterator(Set set){
        long start = System.currentTimeMillis();
        Iterator<Integer> iterator = set.iterator();
        while (iterator.hasNext()){
            iterator.next();
        }
        System.out.println("IteratorToArray.useIterator  :"  +  (System.currentTimeMillis() - start));
    }

    public void useToArray(Set set){
        long start = System.currentTimeMillis();
        Integer[] integers = (Integer[]) set.toArray(new Integer[0]);
        for (Integer integer: integers) {

        }
        System.out.println("IteratorToArray.useToArray  :"  +  (System.currentTimeMillis() - start));
    }

    public static void main(String[] args) {
        IteratorToArray iteratorToArray = new IteratorToArray();

        Set set = iteratorToArray.addHashSet();

        iteratorToArray.useIterator(set);

        iteratorToArray.useToArray(set);

    }
}
